package leetCode.sample;

import org.junit.Test;

import java.util.*;

public class TwoSumSolution {

    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> numMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            numMap.put(nums[i], i);
        }
        for (int i = 0; i < nums.length; i++) {
            int sub = target - nums[i];
            if (numMap.containsKey(sub) && i != numMap.get(sub)) {
                return new int[]{i, numMap.get(sub)};
            }
        }
        return null;
    }

    public int[] twoSumOnceHash(int[] nums, int target) {
        Map<Integer, Integer> numMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int sub = target - nums[i];
            if (numMap.containsKey(sub) && i != numMap.get(sub)) {
                return new int[]{i, numMap.get(sub)};
            }
            numMap.put(nums[i], i);
        }
        return null;
    }

    @Test
    public void testNums() {
        int[] ints = twoSumOnceHash(new int[]{3, 1, 3}, 6);
        Arrays.stream(ints).forEach(i -> System.out.println(i));
    }

    @Test
    public void test() {
        Map<Integer, Integer> numMap = new HashMap<>();
        numMap.put(3, 1);
        numMap.put(3, 2);
        int[] nums = new int[]{3, 3};
        for (int i = 0; i < nums.length; i++) {
            numMap.put(nums[i], i);
        }
        System.out.println(numMap.size());
        System.out.println(numMap.get(3));

    }
}
